package com.rc.evcharger.dto.terminal;


import com.rc.evcharger.common.annotation.ByteLength;
import com.rc.evcharger.common.terminal.RecordSource;
import com.rc.evcharger.common.terminal.Result;
import com.rc.evcharger.common.utils.DataUtil;
import com.rc.evcharger.common.utils.StringUtil;

import java.lang.reflect.Field;
import java.math.BigDecimal;

/**
 * 电桩充电时上报数据，返回数据项格式
 * @author peter
 */
public class ChargeBalaceResult extends RecordSource implements Result {
    private Long pileId;

    @ByteLength(length = 1)
    private String gunNum; // 枪号 1：A枪 2：B枪
    @ByteLength(length = 15)
    private String orderNum; // 订单号;
    @ByteLength(length = 3, decimal = 2)
    private BigDecimal voltage;// 电压
    @ByteLength(length = 3, decimal = 2)
    private BigDecimal elec; // 电流
    @ByteLength(length = 4, decimal = 2)
    private BigDecimal power; // 电量；
    @ByteLength(length = 4)
    private String powerObl; //保留的电量字节
    @ByteLength(length = 1)
    private BigDecimal soc; // 电量百分比；
    @ByteLength(length = 17)
    private String svin; // vin码；
    @ByteLength(length = 1)
    private String elecPileStopReason; // 桩体停止原因
    @ByteLength(length = 4, decimal = 2)
    private BigDecimal powerA; // A枪总电量;
    @ByteLength(length = 4, decimal = 2)
    private BigDecimal powerB; // B枪总电量;

    public ChargeBalaceResult() {
        super();
    }

    public ChargeBalaceResult(String source) {
        super();
        this.source = source;
    }

    public Long getPileId() {
        return pileId;
    }

    public void setPileId(Long pileId) {
        this.pileId = pileId;
    }

    /**
     * 遍历所有字段，按顺序和长度截取数据
     * @param str    数据域
     * @param source 硬件产商
     */
    public ChargeBalaceResult(String str, String source) {
        try {
            Field[] fields = this.getClass().getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                ByteLength bl = fields[i].getAnnotation(ByteLength.class);
                if (bl == null) {
                    continue;
                }
                int len = bl.length() * 2 + bl.length() - 1;
                String value = str.substring(0, len);
                if (str.length() > len) {
                    str = str.substring(len + 1);
                }
                setValue(fields[i], bl, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getGunNum() {
        return gunNum;
    }

    public void setGunNum(String gunNum) {
        this.gunNum = gunNum;
    }

    public String getOrderNum() {
        return orderNum;
    }

    public void setOrderNum(String orderNum) {
        this.orderNum = orderNum;
    }

    public BigDecimal getVoltage() {
        return voltage;
    }

    public void setVoltage(BigDecimal voltage) {
        this.voltage = voltage;
    }

    public BigDecimal getElec() {
        return elec;
    }

    public void setElec(BigDecimal elec) {
        this.elec = elec;
    }

    public BigDecimal getPower() {
        return power;
    }

    public void setPower(BigDecimal power) {
        this.power = power;
    }

    public String getPowerObl() {
        return powerObl;
    }

    public void setPowerObl(String powerObl) {
        this.powerObl = powerObl;
    }

    public BigDecimal getSoc() {
        return soc;
    }

    public void setSoc(BigDecimal soc) {
        this.soc = soc;
    }

    public String getSvin() {
        return svin;
    }

    public void setSvin(String svin) {
        this.svin = svin;
    }

    public String getElecPileStopReason() {
        return elecPileStopReason;
    }

    public void setElecPileStopReason(String elecPileStopReason) {
        this.elecPileStopReason = elecPileStopReason;
    }

    public BigDecimal getPowerA() {
        return powerA;
    }

    public void setPowerA(BigDecimal powerA) {
        this.powerA = powerA;
    }

    public BigDecimal getPowerB() {
        return powerB;
    }

    public void setPowerB(BigDecimal powerB) {
        this.powerB = powerB;
    }

    @Override
    public ChargeBalaceResult getResult(String dataStr) {
        ChargeBalaceResult cbr = new ChargeBalaceResult(dataStr, this.source);
        return cbr;
    }

    private void setValue(Field field, ByteLength bl, String value) {
        try {
            if (field.getType().equals(BigDecimal.class) && value.length() == 2) {
                field.set(this, DataUtil.getBDValue(value, bl.decimal(), true));
            } else if (field.getType().equals(BigDecimal.class)) {
                field.set(this, DataUtil.getBDValue(value, bl.decimal(), false));
            } else if ((field.getName().equals("orderNum"))) {
                String orderNum = StringUtil.getOrderNum(value);
                field.set(this, orderNum);
            } else if ((field.getName().equals("svin"))) {
                field.set(this, StringUtil.resolveVin(value));
            } else {
                field.set(this, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        String A6 = "01 00 00 44 01 19 02 13 14 17 09 19 00 27 41 83 68 00 50 70 03 14 00 00 99 10 00 00 00 00 00 00 " +
                "64 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 00 00 00";
        String A7 = "01 00 00 44 01 19 02 13 14 17 09 19 00 27 41 83 68 00 50 70 03 14 00 00 99 10 00 00 00 00 00 00 " +
                "64 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 24 00 00 00 24 00 00 00";
        ChargeBalaceResult cbr = new ChargeBalaceResult(A6, "");
        ChargeBalaceResult cbr2 = new ChargeBalaceResult(A6, "");
        System.out.println(cbr.toString() + "  " + A6.length() + "  " + A7.length());
        System.out.println(cbr2.toString() + "  " + A6.length() + "  " + A7.length());
        System.out.println(1);
    }
}
